﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Zhp.Entity.Message.Email;
using Zhp.IService.Formula;
using Zhp.IService.Message.Email;
using Zhp.IService.Message.Email.VariableClass;
using Zhp.Model.Sys;
using Zhp.Service.Workflow.Notify;
using Zhp.SqlSugar.Repository;

namespace Zhp.Api.Controllers.Sys
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IndexController : ControllerBase
    {
        private readonly ILogger<IndexController> _logger;
        private readonly IFormulaTestService _formulaTestService;
        private readonly IEmailSender _emailSender;
        private readonly IEmailTemplateRender _emailTemplateRender;
        private readonly IApproveNotifyService _approveNotifyService;
        private readonly IBaseRepository<EmailAccount> _emailAccountRep;
        private readonly IBaseRepository<EmailTemplate> _emailTemplateRep;
        public IndexController(ILogger<IndexController> logger, IFormulaTestService formulaTestService, IApproveNotifyService approveNotifyService, IBaseRepository<EmailAccount> emailAccountRep, IBaseRepository<EmailTemplate> emailTemplateRep, IEmailSender emailSender, IEmailTemplateRender emailTemplateRender)
        {
            _logger = logger;
            _formulaTestService = formulaTestService;
            _approveNotifyService = approveNotifyService;
            _emailAccountRep = emailAccountRep;
            _emailTemplateRep = emailTemplateRep;
            _emailSender = emailSender;
            _emailTemplateRender = emailTemplateRender;
        }

        [AllowAnonymous]
        [HttpGet]
        public async Task<IndexModel> App()
        {
            //var log = new Logs
            //{
            //    TimeStamp = DateTime.Now,
            //    Message = "Test",
            //    LogLevel = "error",
            //};
            //var user = new ExceptionLog
            //{
            //    Controller = "Index",
            //    Action = "App",
            //    ExceptionMessage = "Test"
            //};

            //log.DeleteExtraEntity.Add(new ExceptionLog { Id = 1813504562867343360 });
            //log.Insert();

            //for (int i = 0; i < 10000; i++)
            //{
            //    //_logger.LogDebug("LogDebug");
            //    //_logger.LogInformation("LogInformation");
            //    //_logger.LogTrace("LogTrace");
            //    //_logger.LogWarning("LogWarning");
            //    _logger.LogCritical("LogCritical");
            //    _logger.LogError("LogError");
            //}

            //int a = 0;
            //int b = 1 / a;
            _logger.LogInformation("测试job消息");

            var index = new IndexModel
            {
                App = new App
                {
                    Key = "Zhp",
                    Name = "Zhp",
                    Description = "后台管理系统"
                },
                User = new User
                {
                    Key = "admin",
                    Name = "zhp",
                    Email = "1395629800@qq.com",
                },
                Menu = new Menu[]
                {
                    new Menu
                    {
                        Text = "系统管理",
                        Group = true,
                        HideInBreadcrumb = true,
                        Children = new Menu[]
                        {
                            new Menu
                            {
                                Text = "用户管理",
                                Link = "sys/list",
                            }
                        }
                    }
                }
            };

            //获取菜单数据
            //var path = Path.Combine(AppContext.BaseDirectory, "wwwroot/assets/tmp/app-data.json");
            //var menu = System.IO.File.ReadAllText(path);
            //index = JsonConvert.DeserializeObject<IndexModel>(menu);

            return await Task.FromResult(index);
        }

        [AllowAnonymous]
        [HttpGet]
        public async Task<bool> SendEmail()
        {
            var tpl = await _emailTemplateRep.GetFirstAsync(x => x.Name == "测试邮件");
            var account = await _emailAccountRep.GetByIdAsync(tpl.EmailAccountId);

            var data = new TestEmail
            {
                ToUserName = "纯洁的逗比",
                ToEmail = "123456@qq.com",
                Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                UserList = new List<UserInfo>
                {
                    new UserInfo
                    {
                        UserName = "张三",
                        Birthday = DateTime.Now.ToString("yyyy-MM-dd"),
                    },
                    new UserInfo
                    {
                        UserName = "李四",
                        Birthday = DateTime.Now.ToString("yyyy-MM-dd"),
                    },
                    new UserInfo
                    {
                        UserName = "王五",
                        Birthday = DateTime.Now.ToString("yyyy-MM-dd"),
                    }
                },
                StudentList = new List<StudentInfo>
                {
                    new StudentInfo
                    {
                        Name = "张三",
                        Score = 100,
                    },
                    new StudentInfo
                    {
                        Name = "李四",
                        Score = 99,
                    },
                    new StudentInfo
                    {
                        Name = "王五",
                        Score = 59,
                    }
                }
            };

            tpl = await _emailTemplateRender.RenderTemplateAsync(tpl, data);
            var ret = await _emailSender.SendEmailAsync(account, tpl.Subject, tpl.Body, "1395629800@qq.com", "纯洁的逗比");
            return await Task.FromResult(ret);
        }

        [AllowAnonymous]
        [HttpGet]
        public async Task WorkflowNotify()
        {
            await _approveNotifyService.SendNotify();
        }

        [AllowAnonymous]
        [HttpGet]
        public async Task<bool> Calc(long id)
        {
            return await _formulaTestService.Calc(id);
        }
    }

}
